class Node {
    int data;
    Node next;

    public Node(int data) {
        this.data = data;
    }
}

class SingleCircularLinkedList {
    private Node head;
    private int length;

    public SingleCircularLinkedList() {
        head = new Node(0);
        head.next = head;
        length = 0;
    }

    // 新增：获取头节点的公共方法
    public Node getHead() {
        return head;
    }

    // 头插法
    public void headInsert(int data) {
        Node newNode = new Node(data);
        newNode.next = head.next;
        head.next = newNode;
        length++;
    }

    // 尾插法
    public void tailInsert(int data) {
        Node newNode = new Node(data);
        Node temp = head;
        while (temp.next != head) {
            temp = temp.next;
        }
        temp.next = newNode;
        newNode.next = head;
        length++;
    }

    // 删除节点
    public boolean deleteNode(int data) {
        Node temp = head;
        while (temp.next != head) {
            if (temp.next.data == data) {
                temp.next = temp.next.next;
                length--;
                return true;
            }
            temp = temp.next;
        }
        return false;
    }

    // 反转链表
    public void reverseList() {
        if (head.next == head || head.next.next == head) {
            return;
        }
        Node prev = head.next;
        Node curr = prev.next;
        Node next;
        prev.next = head;
        while (curr != head) {
            next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        head.next = prev;
    }

    // 打印链表
    public void printList() {
        if (head.next == head) {
            System.out.println("空链表");
            return;
        }
        Node temp = head.next;
        StringBuilder sb = new StringBuilder();
        while (temp != head) {
            sb.append(temp.data).append(" , ");
            temp = temp.next;
        }
        if (sb.length() > 2) {
            sb.setLength(sb.length() - 3);
        }
        System.out.println(sb.toString());
    }

    public int getLength() {
        return length;
    }
}

class CircularLinkedListDemo {
    public static void main(String[] args) {
        SingleCircularLinkedList list = new SingleCircularLinkedList();

        System.out.println("----------初始化链表----------");
        // 修改此处：通过公共方法获取 head
        System.out.println("head= " + list.getHead().hashCode());

        System.out.println("----------增加结点(头插法)----------");
        list.headInsert(13);
        list.headInsert(14);
        list.headInsert(15);
        list.printList();
        System.out.println("length: " + list.getLength());

        System.out.println("----------增加结点(尾插法)----------");
        list.tailInsert(21);
        list.tailInsert(22);
        list.tailInsert(23);
        list.printList();
        System.out.println("length: " + list.getLength());

        System.out.println("----------删除结点----------");
        System.out.println("删除数据为5120的结点");
        System.out.println("删除前：");
        list.printList();
        boolean deleteResult = list.deleteNode(5120);
        System.out.println(deleteResult ? "删除成功" : "删除失败");
        list.printList();
        System.out.println("length: " + list.getLength());

        System.out.println("删除数据为13的结点");
        System.out.println("删除前：");
        list.printList();
        deleteResult = list.deleteNode(13);
        System.out.println(deleteResult ? "删除成功" : "删除失败");
        list.printList();
        System.out.println("length: " + list.getLength());

        System.out.println("删除数据为23的结点");
        System.out.println("删除前：");
        list.printList();
        deleteResult = list.deleteNode(23);
        System.out.println(deleteResult ? "删除成功" : "删除失败");
        list.printList();
        System.out.println("length: " + list.getLength());

        System.out.println("----------反转链表----------");
        System.out.println("反转前");
        list.printList();
        list.reverseList();
        System.out.println("反转后");
        list.printList();
    }
}